package com.concurrent.sample.business.event.assemble;

import com.concurrent.sample.business.common.enums.ContractStatusEnum;
import com.concurrent.sample.business.common.enums.EventTypeEnum;
import com.concurrent.sample.business.common.enums.FlowTypeEnum;
import com.concurrent.sample.business.common.enums.OptionEventTypeEnum;
import com.concurrent.sample.business.event.dto.loan.request.LoanOpenRequest;
import com.concurrent.sample.business.event.entity.EventEntity;
import com.concurrent.sample.business.event.param.OptionEvent;
import com.concurrent.sample.business.event.param.loan.LoanOpenOptionEventParam;
import com.concurrent.sample.business.loan.entity.LoanEntity;
import com.concurrent.sample.business.loan.entity.LoanPositionEntity;
import com.concurrent.sample.business.loan.entity.LoanTicketEntity;
import java.time.LocalDate;

public class LoanEventAssemble {

  public static EventEntity assembleOpenLoanEvent(LoanOpenRequest openParam, LoanTicketEntity loanTicket){
    EventEntity event = new EventEntity()
        .setTradingDay(LocalDate.now())
        .setEventType(EventTypeEnum.LOAN)
        .setFlowStatus(FlowTypeEnum.COMMITTED)
        .setCustomerNo(openParam.getCustomerNo())
        .setSymbol(loanTicket.getSymbol())
        .setQuantity(openParam.getQuantity())
        .setLoanTicketId(loanTicket.getId())
        ;
    return event;
  }

  public static LoanEntity assembleOpenLoan(EventEntity openLoanEvent){
    LoanEntity loan = new LoanEntity()
        .setTradingDay(openLoanEvent.getTradingDay())
        .setLoanTicketId(openLoanEvent.getLoanTicketId())
        .setCustomerNo(openLoanEvent.getCustomerNo())
        .setSymbol(openLoanEvent.getSymbol())
        .setInitQty(openLoanEvent.getQuantity())
        .setLiveQty(openLoanEvent.getQuantity())
        .setContractStatus(ContractStatusEnum.LIVE)
        ;
    return loan;
  }

  public static LoanPositionEntity assembleLoanPosition(LoanEntity loanEntity){
    LoanPositionEntity loanPosition = new LoanPositionEntity()
        .setTradingDay(loanEntity.getTradingDay())
        .setCustomerNo(loanEntity.getCustomerNo())
        .setSymbol(loanEntity.getSymbol())
        ;
    return loanPosition;
  }

  public static OptionEvent assembleLoanOpenEvent(EventEntity event){
    LoanOpenOptionEventParam loanOpenEventParam = new LoanOpenOptionEventParam()
        .setEventId(event.getId());

    loanOpenEventParam.setRouteKey(event.getSymbol());

    OptionEvent optionEvent = new OptionEvent()
        .setEventType(OptionEventTypeEnum.LOAN_OPEN)
        .setParam(loanOpenEventParam);

    return optionEvent;
  }

}
